<?php
namespace Forms\Filter;

use Pinenut\Form\Filter;

class TranslitAction extends Filter {

	protected static $_translitTable = [
		'А' => 'A',
		'а' => 'a',
		'Б' => 'B',
		'б' => 'b',
		'В' => 'V',
		'в' => 'v',
		'Г' => 'G',
		'г' => 'g',
		'Д' => 'D',
		'д' => 'd',
		'Е' => 'E',
		'е' => 'e',
		'Ё' => 'E',
		'ё' => 'e',
		'Ж' => 'Zh',
		'ж' => 'zh',
		'З' => 'Z',
		'з' => 'z',
		'И' => 'I',
		'и' => 'i',
		'Й' => 'Y',
		'й' => 'y',
		'К' => 'K',
		'к' => 'k',
		'Л' => 'L',
		'л' => 'l',
		'М' => 'M',
		'м' => 'm',
		'Н' => 'N',
		'н' => 'n',
		'О' => 'O',
		'о' => 'o',
		'П' => 'P',
		'п' => 'p',
		'Р' => 'R',
		'р' => 'r',
		'С' => 'S',
		'с' => 's',
		'Т' => 'T',
		'т' => 't',
		'У' => 'U',
		'у' => 'u',
		'Ф' => 'F',
		'ф' => 'f',
		'Х' => 'H',
		'х' => 'h',
		'Ц' => 'Ts',
		'ц' => 'ts',
		'Ч' => 'Ch',
		'ч' => 'ch',
		'Ш' => 'Sh',
		'ш' => 'sh',
		'Щ' => 'Sh',
		'щ' => 'sh',
		'Ъ' => '',
		'ъ' => '',
		'Ы' => 'I',
		'ы' => 'i',
		'Ь' => '',
		'ь' => '',
		'Э' => 'E',
		'э' => 'e',
		'Ю' => 'Yu',
		'ю' => 'yu',
		'Я' => 'Ya',
		'я' => 'ya'
	];
	
	static protected $_translitTableKeys, $_translitTableValues;

	public function run( ) {
		return static::translit($this->request->value);
	}
	static function translit($str) {
		if (empty(static::$_translitTableKeys) || empty(static::$_translitTableValues)) {
			static::$_translitTableKeys = array_keys(static::$_translitTable);
			static::$_translitTableValues = array_values(static::$_translitTable);
		}
		
		return str_replace(static::$_translitTableKeys, static::$_translitTableValues, $str);
	}
}